package com.wzt.base.service.impl;

import com.wzt.base.mapper.BaseMapper;
import com.wzt.base.query.BaseQuery;
import com.wzt.base.service.IBaseService;
import com.wzt.base.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Zenith
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional(rollbackFor = {})
    public void addOne(T t) {
        mapper.insert(t);
    }

    @Override
    @Transactional(rollbackFor = {})
    public void removeOne(Long id) {
        mapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = {})
    public void removeMul(List<Long> ids) {
        mapper.deleteBatch(ids);
    }

    @Override
    @Transactional(rollbackFor = {})
    public void editOne(T t) {
        mapper.update(t);
    }

    @Override
    public T getOne(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return mapper.loadAll();
    }

    @Override
    public PageResult<T> getPage(BaseQuery query) {
        Long total = mapper.loadCounts(query);
        List<T> data = mapper.loadPageData(query);
        PageResult<T> result = new PageResult<>();
        result.setTotal(total);
        result.setRows(data);
        return result;
    }
}
